/****************************************************************************************************
Do file name: 0_Master.do
Author: Thiago Scot
This version: November 2022
Description: Master do-file that runs all other dos to construct databases and perform analysis for
"Selecting Top Bureaucrats: Admission Exams and Performance in Brazil"
****************************************************************************************************/

clear all
set more off

**Install packages
/*
ssc install tabout
ssc install matchit
ssc install tsspell
ssc install group_twoway
ssc install gtools
ssc install freqindex
*/

***** Set up user name and paths
if "`c(username)'" == "wb463689" {
	global root "/Users/wb463689/Dropbox/Juizes Brasil/Paper_Selection/submissions/Restat_Submission/acceptance/materials"
} 

if "`c(username)'" == "thiagoscott" {
	global root "/Users/thiagoscott/Dropbox/Juizes Brasil/Paper_Selection/submissions/Restat_Submission/acceptance/materials"
} 

global do            "$root/code"
global data_input    "$root/input"
global data_out      "$root/output/data"
global temp          "$root/temp"
global out           "$root/output"
global paper_figures "$out/figures"
global paper_tables  "$out/tables"

	
do "$do/2_ConcursoGradesClean.do"					//Cleans Concurso database, names, grades, etc

do "$do/3a_CleanCNJ_ForMerge.do"					//Cleaning CNJ productivity database

do "$do/3_MergeStrings.do"							//Matches data from concurso and CNJ using string name (takes long time to run)

do "$do/4_MergeCleaning.do"							//Cleans Concurso database after matching, prepares for later merge with CNJ database

do "$do/5_DescriptivesAKM.do"						//Produce initial descriptive stats

do "$do/6_MonthLevel_results.do"					//Regressions at monthly-judge-court level

*****Estimate FE with R here! -->
*rsource using "$do/AKM_RScript_Estimate.R", roptions("--vanilla") rp("/Library/Frameworks/R.framework/Resources/bin/R")

do "$do/7_Extra_SPData.do"							//Bring additional data for State of Sao Paulo 

do "$do/8_AKM_PrepareJudgeLevel.do"					//Prepare data at judge level

do "$do/9_AKM_MainResults.do"						//Key results on correlation between FE and grades at judge-level

do "$do/10_EventStudy.do"							//Event-study graph using clean transitions
